﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NOSDC.Extensions;
using NOSDC.Services;
using NOSDC.Models;
using NOSDC.Utility;

namespace NOSDC.Controllers
{
    public class HomeController : Controller
    {
        private readonly IArticleService _service;

        public HomeController()
        {
            _service = new ArticleService();
        }

        // GET: /Home/
        [OutputCache(Duration = 1, VaryByParam = "none")]
        public ActionResult Index()
        {
            var model = new FrontPageViewModel {Article = _service.GetFrontpageArticles()};

            return View(model);
        }

        public ActionResult Rss()
        {
            var news = _service.GetFrontpageArticles();
            IList<IRss> rssItems = new List<IRss>();
            news.ToList().ForEach(x =>  rssItems.Add(new RssModel(x.Title, x.Story)));

            return new RssResult(rssItems, "Northern Ontario Software Developers Community", "NOSDC");
        }


        // GET: /Home/ContactUs
        public ActionResult ContactUs()
        {
            var model = new ContactUsViewModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult ContactUs(ContactUsViewModel model)
        {
            if (ModelState.IsValid)
            {
                Mail.SendContactEmail(model.ContactUs);
                return RedirectToAction("Index", "Home");
            }

            return View(model);
        }

        // GET: /Home/AboutUs
        public ActionResult AboutUs()
        {
            return View();
        }
    }
}
